package com.chinasofti.controlmongodb.controller;

import com.chinasofti.controlmongodb.dao.MongoUserDao;
import com.chinasofti.controlmongodb.model.MongoUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MongoUserController {

    @Autowired
    private MongoUserDao mtdao;

    @GetMapping(value="/addMongoUser")
    public String saveUser() throws Exception {
        MongoUser mgUser=new MongoUser();
        mgUser.setId((int) (Math.random()*100));
        mgUser.setAge(18);
        mgUser.setName("chinasofti");
        mtdao.saveUser(mgUser);
        return "add mgUser success";
    }

    @GetMapping(value="/queryMongoUser")
    public MongoUser findUserByName(){
        MongoUser mgUser= mtdao.findUserByName("chinasofti");
        System.out.println("myName is "+mgUser);
        return mgUser;
    }

    @GetMapping(value="/updateMongoUser")
    public void updateUser(){
        MongoUser mgUser=new MongoUser();
        mgUser.setId(12);
        mgUser.setAge(144);
        mgUser.setName("chinasoft");
        mtdao.updateUser(mgUser);
    }

    @GetMapping(value="/deleteMongoUser")
    public void deleteUserById(){
        mtdao.deleteUserById(0);
    }

}
